#include "titlebar.h"
#include "ui_titlebar.h"

#include <QPainter>
#include <QMouseEvent>

TitleBar::TitleBar(const QString filePath, const QString content,
                   ButtonStyle style, QColor color, QWidget *parent) :
    QWidget(parent), ui(new Ui::TitleBar), color(color), moveable(false)
{
    ui->setupUi(this);

    init(filePath, content, style);
}

TitleBar::~TitleBar()
{
    delete ui;
}

void TitleBar::init(const QString filePath, const QString content, ButtonStyle style)
{
    setTitleIcon(filePath);
    setTitleContent(content);
    setButtonStyle(style);

    initStyleSheet();
}

void TitleBar::initStyleSheet(const QString path)
{
    this->setStyleSheet(QString("QPushButton#pushButtonClose { \
                             border-image: url(%1/close.png) 0 81 0 0; } \
                         QPushButton#pushButtonClose:hover { \
                             border-image: url(%1/close.png) 0 54 0 27; } \
                         QPushButton#pushButtonClose:pressed { \
                             border-image: url(%1/close.png) 0 27 0 54; } \
                         QPushButton#pushButtonMax { \
                             border-image: url(%1/max.png) 0 81 0 0; } \
                         QPushButton#pushButtonMax:hover { \
                             border-image: url(%1/max.png) 0 54 0 27; } \
                         QPushButton#pushButtonMax:pressed { \
                             border-image: url(%1/max.png) 0 27 0 54; } \
                         QPushButton#pushButtonMin { \
                             border-image: url(%1/min.png) 0 81 0 0; } \
                         QPushButton#pushButtonMin:hover { \
                             border-image: url(%1/min.png) 0 54 0 27; } \
                         QPushButton#pushButtonMin:pressed { \
                             border-image: url(%1/min.png) 0 27 0 54; } \
                         QPushButton#pushButtonRestore { \
                             border-image: url(%1/restore.png) 0 81 0 0; } \
                         QPushButton#pushButtonRestore:hover { \
                             border-image: url(%1/restore.png) 0 54 0 27; } \
                         QPushButton#pushButtonRestore:pressed { \
                             border-image: url(%1/restore.png) 0 27 0 54; } \
                         ").arg(path));
}

void TitleBar::setTitleIcon(const QString filePath)
{
    QPixmap image(filePath);
    image = image.scaled(ui->labelLogo->size(),
                         Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    ui->labelLogo->setPixmap(image);
}

void TitleBar::setTitleContent(const QString content, QColor color)
{
    ui->labelTitle->setText(content);

    QFont font;
    font.setFamily("Microsoft YaHei");
    ui->labelTitle->setFont(font);

    QPalette palette = ui->labelTitle->palette();
    palette.setColor(QPalette::WindowText, color);
    ui->labelTitle->setPalette(palette);
}

void TitleBar::setButtonStyle(ButtonStyle style)
{
    /* ***********************
     * MIN_MAX_CLOSE_BUTTON,
     * CLOSE_BUTTON
     * ***********************/
    this->style = style;

    switch(style) {
    case ButtonStyle::MIN_MAX_CLOSE_BUTTON:
        ui->pushButtonRestore->setVisible(false);
        ui->pushButtonMin->setVisible(true);
        ui->pushButtonMax->setVisible(true);
        ui->pushButtonClose->setVisible(true);
        break;
    case ButtonStyle::CLOSE_BUTTON:
        ui->pushButtonRestore->setVisible(false);
        ui->pushButtonMin->setVisible(false);
        ui->pushButtonMax->setVisible(false);
        ui->pushButtonClose->setVisible(true);
        break;
    }
}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
    if (style == ButtonStyle::MIN_MAX_CLOSE_BUTTON
            && ui->pushButtonRestore->isVisible()) {
        // 如果窗口最大化, 禁止拖动
    } else {
        moveable = true;
        titleBarStartPos = event->globalPos();
    }
    return QWidget::mousePressEvent(event);
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if (moveable) {
        QPoint movePos = event->globalPos() - titleBarStartPos;
        QPoint widgetPos = this->parentWidget()->parentWidget()->pos();
        QPoint currentPos = widgetPos + movePos;
        this->parentWidget()->parentWidget()->move(currentPos);

        titleBarStartPos = event->globalPos();
    }

    return QWidget::mouseMoveEvent(event);
}

void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    moveable = false;
    return QWidget::mouseReleaseEvent(event);
}

void TitleBar::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setBrush(color);
    painter.setPen(Qt::NoPen);
    painter.drawRect(this->rect());


    QWidget::paintEvent(event);
}

void TitleBar::on_pushButtonMax_clicked()
{
    ui->pushButtonMax->setVisible(false);
    ui->pushButtonRestore->setVisible(true);

    emit maxButtonClicked();
}

void TitleBar::on_pushButtonRestore_clicked()
{
    ui->pushButtonMax->setVisible(true);
    ui->pushButtonRestore->setVisible(false);

    emit restoreButtonClicked();
}

void TitleBar::on_pushButtonMin_clicked()
{
    emit minButtonClicked();
}

void TitleBar::on_pushButtonClose_clicked()
{
    emit closeButtonClicked();
}
